
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>django.db.migrations.operations.models &#8212; Django 1.11.22.dev20190603194737 documentation</title>
    <link rel="stylesheet" href="../../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../../../" src="../../../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../../search.html" />



 
<script type="text/javascript" src="../../../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../../../index.html">Django 1.11.22.dev20190603194737 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-db-migrations-operations-models">
            
  <h1>Source code for django.db.migrations.operations.models</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">__future__</span> <span class="k">import</span> <span class="n">unicode_literals</span>

<span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>
<span class="kn">from</span> <span class="nn">django.db.migrations.operations.base</span> <span class="k">import</span> <span class="n">Operation</span>
<span class="kn">from</span> <span class="nn">django.db.migrations.state</span> <span class="k">import</span> <span class="n">ModelState</span>
<span class="kn">from</span> <span class="nn">django.db.models.fields.related</span> <span class="k">import</span> <span class="n">RECURSIVE_RELATIONSHIP_CONSTANT</span>
<span class="kn">from</span> <span class="nn">django.db.models.options</span> <span class="k">import</span> <span class="n">normalize_together</span>
<span class="kn">from</span> <span class="nn">django.utils</span> <span class="k">import</span> <span class="n">six</span>
<span class="kn">from</span> <span class="nn">django.utils.functional</span> <span class="k">import</span> <span class="n">cached_property</span>

<span class="kn">from</span> <span class="nn">.fields</span> <span class="k">import</span> <span class="p">(</span>
    <span class="n">AddField</span><span class="p">,</span> <span class="n">AlterField</span><span class="p">,</span> <span class="n">FieldOperation</span><span class="p">,</span> <span class="n">RemoveField</span><span class="p">,</span> <span class="n">RenameField</span><span class="p">,</span>
<span class="p">)</span>


<span class="k">def</span> <span class="nf">_check_for_duplicates</span><span class="p">(</span><span class="n">arg_name</span><span class="p">,</span> <span class="n">objs</span><span class="p">):</span>
    <span class="n">used_vals</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
    <span class="k">for</span> <span class="n">val</span> <span class="ow">in</span> <span class="n">objs</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">val</span> <span class="ow">in</span> <span class="n">used_vals</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;Found duplicate value </span><span class="si">%s</span><span class="s2"> in CreateModel </span><span class="si">%s</span><span class="s2"> argument.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">val</span><span class="p">,</span> <span class="n">arg_name</span><span class="p">)</span>
            <span class="p">)</span>
        <span class="n">used_vals</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">val</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">ModelOperation</span><span class="p">(</span><span class="n">Operation</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>

    <span class="nd">@cached_property</span>
    <span class="k">def</span> <span class="nf">name_lower</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">references_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">app_label</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">name</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">name_lower</span>

    <span class="k">def</span> <span class="nf">reduce</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">operation</span><span class="p">,</span> <span class="n">in_between</span><span class="p">,</span> <span class="n">app_label</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="nb">super</span><span class="p">(</span><span class="n">ModelOperation</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">reduce</span><span class="p">(</span><span class="n">operation</span><span class="p">,</span> <span class="n">in_between</span><span class="p">,</span> <span class="n">app_label</span><span class="o">=</span><span class="n">app_label</span><span class="p">)</span> <span class="ow">or</span>
            <span class="ow">not</span> <span class="n">operation</span><span class="o">.</span><span class="n">references_model</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">app_label</span><span class="p">)</span>
        <span class="p">)</span>


<div class="viewcode-block" id="CreateModel"><a class="viewcode-back" href="../../../../../ref/migration-operations.html#django.db.migrations.operations.CreateModel">[docs]</a><span class="k">class</span> <span class="nc">CreateModel</span><span class="p">(</span><span class="n">ModelOperation</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Create a model&#39;s table.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">serialization_expand_args</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;fields&#39;</span><span class="p">,</span> <span class="s1">&#39;options&#39;</span><span class="p">,</span> <span class="s1">&#39;managers&#39;</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">fields</span><span class="p">,</span> <span class="n">options</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">bases</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">managers</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fields</span> <span class="o">=</span> <span class="n">fields</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">options</span> <span class="o">=</span> <span class="n">options</span> <span class="ow">or</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">bases</span> <span class="o">=</span> <span class="n">bases</span> <span class="ow">or</span> <span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">,)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">managers</span> <span class="o">=</span> <span class="n">managers</span> <span class="ow">or</span> <span class="p">[]</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">CreateModel</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
        <span class="c1"># Sanity-check that there are no duplicated field names, bases, or</span>
        <span class="c1"># manager names</span>
        <span class="n">_check_for_duplicates</span><span class="p">(</span><span class="s1">&#39;fields&#39;</span><span class="p">,</span> <span class="p">(</span><span class="n">name</span> <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">_</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">fields</span><span class="p">))</span>
        <span class="n">_check_for_duplicates</span><span class="p">(</span><span class="s1">&#39;bases&#39;</span><span class="p">,</span> <span class="p">(</span>
            <span class="n">base</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">label_lower</span> <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">base</span><span class="p">,</span> <span class="s1">&#39;_meta&#39;</span><span class="p">)</span> <span class="k">else</span>
            <span class="n">base</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">base</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">)</span> <span class="k">else</span> <span class="n">base</span>
            <span class="k">for</span> <span class="n">base</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">bases</span>
        <span class="p">))</span>
        <span class="n">_check_for_duplicates</span><span class="p">(</span><span class="s1">&#39;managers&#39;</span><span class="p">,</span> <span class="p">(</span><span class="n">name</span> <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">_</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">managers</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">deconstruct</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">kwargs</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
            <span class="s1">&#39;fields&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">fields</span><span class="p">,</span>
        <span class="p">}</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="p">:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;options&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">options</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">bases</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">bases</span> <span class="o">!=</span> <span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">,):</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;bases&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">bases</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">managers</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">managers</span> <span class="o">!=</span> <span class="p">[(</span><span class="s1">&#39;objects&#39;</span><span class="p">,</span> <span class="n">models</span><span class="o">.</span><span class="n">Manager</span><span class="p">())]:</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;managers&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">managers</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
            <span class="p">[],</span>
            <span class="n">kwargs</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">state_forwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">state</span><span class="p">):</span>
        <span class="n">state</span><span class="o">.</span><span class="n">add_model</span><span class="p">(</span><span class="n">ModelState</span><span class="p">(</span>
            <span class="n">app_label</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
            <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">fields</span><span class="p">),</span>
            <span class="nb">dict</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="p">),</span>
            <span class="nb">tuple</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">bases</span><span class="p">),</span>
            <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">managers</span><span class="p">),</span>
        <span class="p">))</span>

    <span class="k">def</span> <span class="nf">database_forwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">):</span>
        <span class="n">model</span> <span class="o">=</span> <span class="n">to_state</span><span class="o">.</span><span class="n">apps</span><span class="o">.</span><span class="n">get_model</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">allow_migrate_model</span><span class="p">(</span><span class="n">schema_editor</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">alias</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
            <span class="n">schema_editor</span><span class="o">.</span><span class="n">create_model</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">database_backwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">):</span>
        <span class="n">model</span> <span class="o">=</span> <span class="n">from_state</span><span class="o">.</span><span class="n">apps</span><span class="o">.</span><span class="n">get_model</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">allow_migrate_model</span><span class="p">(</span><span class="n">schema_editor</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">alias</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
            <span class="n">schema_editor</span><span class="o">.</span><span class="n">delete_model</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">describe</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;Create </span><span class="si">%s</span><span class="s2">model </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="s2">&quot;proxy &quot;</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;proxy&quot;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span> <span class="k">else</span> <span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">references_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">app_label</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">name_lower</span> <span class="o">=</span> <span class="n">name</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">name_lower</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">name_lower</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">True</span>

        <span class="c1"># Check we didn&#39;t inherit from the model</span>
        <span class="n">models_to_check</span> <span class="o">=</span> <span class="p">[</span>
            <span class="n">base</span> <span class="k">for</span> <span class="n">base</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">bases</span>
            <span class="k">if</span> <span class="n">base</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">models</span><span class="o">.</span><span class="n">Model</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">base</span><span class="p">,</span> <span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">base</span><span class="o">.</span><span class="n">ModelBase</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">))</span>
        <span class="p">]</span>
        <span class="c1"># Check we have no FKs/M2Ms with it</span>
        <span class="k">for</span> <span class="n">fname</span><span class="p">,</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">fields</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="p">:</span>
                <span class="n">models_to_check</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="p">)</span>
        <span class="c1"># Now go over all the models and check against them</span>
        <span class="k">for</span> <span class="n">model</span> <span class="ow">in</span> <span class="n">models_to_check</span><span class="p">:</span>
            <span class="n">model_app_label</span><span class="p">,</span> <span class="n">model_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_to_key</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">model_name</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="n">name_lower</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">app_label</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">model_app_label</span> <span class="ow">or</span> <span class="n">model_app_label</span> <span class="o">==</span> <span class="n">app_label</span><span class="p">:</span>
                    <span class="k">return</span> <span class="kc">True</span>
        <span class="k">return</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">model_to_key</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Take either a model class or an &quot;app_label.ModelName&quot; string</span>
<span class="sd">        and return (app_label, object_name).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">model</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;.&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span>

    <span class="k">def</span> <span class="nf">reduce</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">operation</span><span class="p">,</span> <span class="n">in_between</span><span class="p">,</span> <span class="n">app_label</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">operation</span><span class="p">,</span> <span class="n">DeleteModel</span><span class="p">)</span> <span class="ow">and</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">name_lower</span> <span class="o">==</span> <span class="n">operation</span><span class="o">.</span><span class="n">name_lower</span> <span class="ow">and</span>
                <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;proxy&quot;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)):</span>
            <span class="k">return</span> <span class="p">[]</span>
        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">operation</span><span class="p">,</span> <span class="n">RenameModel</span><span class="p">)</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">name_lower</span> <span class="o">==</span> <span class="n">operation</span><span class="o">.</span><span class="n">old_name_lower</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[</span>
                <span class="n">CreateModel</span><span class="p">(</span>
                    <span class="n">operation</span><span class="o">.</span><span class="n">new_name</span><span class="p">,</span>
                    <span class="n">fields</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">fields</span><span class="p">,</span>
                    <span class="n">options</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="p">,</span>
                    <span class="n">bases</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">bases</span><span class="p">,</span>
                    <span class="n">managers</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">managers</span><span class="p">,</span>
                <span class="p">),</span>
            <span class="p">]</span>
        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">operation</span><span class="p">,</span> <span class="n">AlterModelOptions</span><span class="p">)</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">name_lower</span> <span class="o">==</span> <span class="n">operation</span><span class="o">.</span><span class="n">name_lower</span><span class="p">:</span>
            <span class="n">new_options</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
            <span class="n">new_options</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">operation</span><span class="o">.</span><span class="n">options</span><span class="p">)</span>
            <span class="k">return</span> <span class="p">[</span>
                <span class="n">CreateModel</span><span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
                    <span class="n">fields</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">fields</span><span class="p">,</span>
                    <span class="n">options</span><span class="o">=</span><span class="n">new_options</span><span class="p">,</span>
                    <span class="n">bases</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">bases</span><span class="p">,</span>
                    <span class="n">managers</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">managers</span><span class="p">,</span>
                <span class="p">),</span>
            <span class="p">]</span>
        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">operation</span><span class="p">,</span> <span class="n">FieldOperation</span><span class="p">)</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">name_lower</span> <span class="o">==</span> <span class="n">operation</span><span class="o">.</span><span class="n">model_name_lower</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">operation</span><span class="p">,</span> <span class="n">AddField</span><span class="p">):</span>
                <span class="c1"># Don&#39;t allow optimizations of FKs through models they reference</span>
                <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">operation</span><span class="o">.</span><span class="n">field</span><span class="p">,</span> <span class="s2">&quot;remote_field&quot;</span><span class="p">)</span> <span class="ow">and</span> <span class="n">operation</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="p">:</span>
                    <span class="k">for</span> <span class="n">between</span> <span class="ow">in</span> <span class="n">in_between</span><span class="p">:</span>
                        <span class="c1"># Check that it doesn&#39;t point to the model</span>
                        <span class="n">app_label</span><span class="p">,</span> <span class="n">object_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_to_key</span><span class="p">(</span><span class="n">operation</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="p">)</span>
                        <span class="k">if</span> <span class="n">between</span><span class="o">.</span><span class="n">references_model</span><span class="p">(</span><span class="n">object_name</span><span class="p">,</span> <span class="n">app_label</span><span class="p">):</span>
                            <span class="k">return</span> <span class="kc">False</span>
                        <span class="c1"># Check that it&#39;s not through the model</span>
                        <span class="k">if</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">operation</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="p">,</span> <span class="s2">&quot;through&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">):</span>
                            <span class="n">app_label</span><span class="p">,</span> <span class="n">object_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_to_key</span><span class="p">(</span><span class="n">operation</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="p">)</span>
                            <span class="k">if</span> <span class="n">between</span><span class="o">.</span><span class="n">references_model</span><span class="p">(</span><span class="n">object_name</span><span class="p">,</span> <span class="n">app_label</span><span class="p">):</span>
                                <span class="k">return</span> <span class="kc">False</span>
                <span class="k">return</span> <span class="p">[</span>
                    <span class="n">CreateModel</span><span class="p">(</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
                        <span class="n">fields</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">fields</span> <span class="o">+</span> <span class="p">[(</span><span class="n">operation</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">operation</span><span class="o">.</span><span class="n">field</span><span class="p">)],</span>
                        <span class="n">options</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="p">,</span>
                        <span class="n">bases</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">bases</span><span class="p">,</span>
                        <span class="n">managers</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">managers</span><span class="p">,</span>
                    <span class="p">),</span>
                <span class="p">]</span>
            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">operation</span><span class="p">,</span> <span class="n">AlterField</span><span class="p">):</span>
                <span class="k">return</span> <span class="p">[</span>
                    <span class="n">CreateModel</span><span class="p">(</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
                        <span class="n">fields</span><span class="o">=</span><span class="p">[</span>
                            <span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">operation</span><span class="o">.</span><span class="n">field</span> <span class="k">if</span> <span class="n">n</span> <span class="o">==</span> <span class="n">operation</span><span class="o">.</span><span class="n">name</span> <span class="k">else</span> <span class="n">v</span><span class="p">)</span>
                            <span class="k">for</span> <span class="n">n</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">fields</span>
                        <span class="p">],</span>
                        <span class="n">options</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="p">,</span>
                        <span class="n">bases</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">bases</span><span class="p">,</span>
                        <span class="n">managers</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">managers</span><span class="p">,</span>
                    <span class="p">),</span>
                <span class="p">]</span>
            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">operation</span><span class="p">,</span> <span class="n">RemoveField</span><span class="p">):</span>
                <span class="k">return</span> <span class="p">[</span>
                    <span class="n">CreateModel</span><span class="p">(</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
                        <span class="n">fields</span><span class="o">=</span><span class="p">[</span>
                            <span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span>
                            <span class="k">for</span> <span class="n">n</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">fields</span>
                            <span class="k">if</span> <span class="n">n</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">!=</span> <span class="n">operation</span><span class="o">.</span><span class="n">name_lower</span>
                        <span class="p">],</span>
                        <span class="n">options</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="p">,</span>
                        <span class="n">bases</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">bases</span><span class="p">,</span>
                        <span class="n">managers</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">managers</span><span class="p">,</span>
                    <span class="p">),</span>
                <span class="p">]</span>
            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">operation</span><span class="p">,</span> <span class="n">RenameField</span><span class="p">):</span>
                <span class="k">return</span> <span class="p">[</span>
                    <span class="n">CreateModel</span><span class="p">(</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
                        <span class="n">fields</span><span class="o">=</span><span class="p">[</span>
                            <span class="p">(</span><span class="n">operation</span><span class="o">.</span><span class="n">new_name</span> <span class="k">if</span> <span class="n">n</span> <span class="o">==</span> <span class="n">operation</span><span class="o">.</span><span class="n">old_name</span> <span class="k">else</span> <span class="n">n</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span>
                            <span class="k">for</span> <span class="n">n</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">fields</span>
                        <span class="p">],</span>
                        <span class="n">options</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="p">,</span>
                        <span class="n">bases</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">bases</span><span class="p">,</span>
                        <span class="n">managers</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">managers</span><span class="p">,</span>
                    <span class="p">),</span>
                <span class="p">]</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">CreateModel</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">reduce</span><span class="p">(</span><span class="n">operation</span><span class="p">,</span> <span class="n">in_between</span><span class="p">,</span> <span class="n">app_label</span><span class="o">=</span><span class="n">app_label</span><span class="p">)</span></div>


<div class="viewcode-block" id="DeleteModel"><a class="viewcode-back" href="../../../../../ref/migration-operations.html#django.db.migrations.operations.DeleteModel">[docs]</a><span class="k">class</span> <span class="nc">DeleteModel</span><span class="p">(</span><span class="n">ModelOperation</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Drops a model&#39;s table.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">deconstruct</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">kwargs</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
        <span class="p">}</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
            <span class="p">[],</span>
            <span class="n">kwargs</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">state_forwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">state</span><span class="p">):</span>
        <span class="n">state</span><span class="o">.</span><span class="n">remove_model</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name_lower</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">database_forwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">):</span>
        <span class="n">model</span> <span class="o">=</span> <span class="n">from_state</span><span class="o">.</span><span class="n">apps</span><span class="o">.</span><span class="n">get_model</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">allow_migrate_model</span><span class="p">(</span><span class="n">schema_editor</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">alias</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
            <span class="n">schema_editor</span><span class="o">.</span><span class="n">delete_model</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">database_backwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">):</span>
        <span class="n">model</span> <span class="o">=</span> <span class="n">to_state</span><span class="o">.</span><span class="n">apps</span><span class="o">.</span><span class="n">get_model</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">allow_migrate_model</span><span class="p">(</span><span class="n">schema_editor</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">alias</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
            <span class="n">schema_editor</span><span class="o">.</span><span class="n">create_model</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">describe</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;Delete model </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="p">)</span></div>


<div class="viewcode-block" id="RenameModel"><a class="viewcode-back" href="../../../../../ref/migration-operations.html#django.db.migrations.operations.RenameModel">[docs]</a><span class="k">class</span> <span class="nc">RenameModel</span><span class="p">(</span><span class="n">ModelOperation</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Renames a model.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">old_name</span><span class="p">,</span> <span class="n">new_name</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">old_name</span> <span class="o">=</span> <span class="n">old_name</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">new_name</span> <span class="o">=</span> <span class="n">new_name</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">RenameModel</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">old_name</span><span class="p">)</span>

    <span class="nd">@cached_property</span>
    <span class="k">def</span> <span class="nf">old_name_lower</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">old_name</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>

    <span class="nd">@cached_property</span>
    <span class="k">def</span> <span class="nf">new_name_lower</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">new_name</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">deconstruct</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">kwargs</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;old_name&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">old_name</span><span class="p">,</span>
            <span class="s1">&#39;new_name&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">new_name</span><span class="p">,</span>
        <span class="p">}</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
            <span class="p">[],</span>
            <span class="n">kwargs</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">_get_model_tuple</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">remote_model</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">model_name</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">remote_model</span> <span class="o">==</span> <span class="n">RECURSIVE_RELATIONSHIP_CONSTANT</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">model_name</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
        <span class="k">elif</span> <span class="s1">&#39;.&#39;</span> <span class="ow">in</span> <span class="n">remote_model</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">remote_model</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">remote_model</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">state_forwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">state</span><span class="p">):</span>
        <span class="c1"># Add a new model.</span>
        <span class="n">renamed_model</span> <span class="o">=</span> <span class="n">state</span><span class="o">.</span><span class="n">models</span><span class="p">[</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">old_name_lower</span><span class="p">]</span><span class="o">.</span><span class="n">clone</span><span class="p">()</span>
        <span class="n">renamed_model</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">new_name</span>
        <span class="n">state</span><span class="o">.</span><span class="n">models</span><span class="p">[</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">new_name_lower</span><span class="p">]</span> <span class="o">=</span> <span class="n">renamed_model</span>
        <span class="c1"># Repoint all fields pointing to the old model to the new one.</span>
        <span class="n">old_model_tuple</span> <span class="o">=</span> <span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">old_name_lower</span>
        <span class="n">new_remote_model</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">.</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">new_name</span><span class="p">)</span>
        <span class="n">to_reload</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="p">(</span><span class="n">model_app_label</span><span class="p">,</span> <span class="n">model_name</span><span class="p">),</span> <span class="n">model_state</span> <span class="ow">in</span> <span class="n">state</span><span class="o">.</span><span class="n">models</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">model_changed</span> <span class="o">=</span> <span class="kc">False</span>
            <span class="k">for</span> <span class="n">index</span><span class="p">,</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">field</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">model_state</span><span class="o">.</span><span class="n">fields</span><span class="p">):</span>
                <span class="n">changed_field</span> <span class="o">=</span> <span class="kc">None</span>
                <span class="n">remote_field</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">remote_field</span>
                <span class="k">if</span> <span class="n">remote_field</span><span class="p">:</span>
                    <span class="n">remote_model_tuple</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_model_tuple</span><span class="p">(</span>
                        <span class="n">remote_field</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">model_app_label</span><span class="p">,</span> <span class="n">model_name</span>
                    <span class="p">)</span>
                    <span class="k">if</span> <span class="n">remote_model_tuple</span> <span class="o">==</span> <span class="n">old_model_tuple</span><span class="p">:</span>
                        <span class="n">changed_field</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">clone</span><span class="p">()</span>
                        <span class="n">changed_field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">new_remote_model</span>
                    <span class="n">through_model</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">remote_field</span><span class="p">,</span> <span class="s1">&#39;through&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
                    <span class="k">if</span> <span class="n">through_model</span><span class="p">:</span>
                        <span class="n">through_model_tuple</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_model_tuple</span><span class="p">(</span>
                            <span class="n">through_model</span><span class="p">,</span> <span class="n">model_app_label</span><span class="p">,</span> <span class="n">model_name</span>
                        <span class="p">)</span>
                        <span class="k">if</span> <span class="n">through_model_tuple</span> <span class="o">==</span> <span class="n">old_model_tuple</span><span class="p">:</span>
                            <span class="k">if</span> <span class="n">changed_field</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                                <span class="n">changed_field</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">clone</span><span class="p">()</span>
                            <span class="n">changed_field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span> <span class="o">=</span> <span class="n">new_remote_model</span>
                <span class="k">if</span> <span class="n">changed_field</span><span class="p">:</span>
                    <span class="n">model_state</span><span class="o">.</span><span class="n">fields</span><span class="p">[</span><span class="n">index</span><span class="p">]</span> <span class="o">=</span> <span class="n">name</span><span class="p">,</span> <span class="n">changed_field</span>
                    <span class="n">model_changed</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="k">if</span> <span class="n">model_changed</span><span class="p">:</span>
                <span class="n">to_reload</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">model_app_label</span><span class="p">,</span> <span class="n">model_name</span><span class="p">))</span>
        <span class="c1"># Reload models related to old model before removing the old model.</span>
        <span class="n">state</span><span class="o">.</span><span class="n">reload_models</span><span class="p">(</span><span class="n">to_reload</span><span class="p">,</span> <span class="n">delay</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="c1"># Remove the old model.</span>
        <span class="n">state</span><span class="o">.</span><span class="n">remove_model</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">old_name_lower</span><span class="p">)</span>
        <span class="n">state</span><span class="o">.</span><span class="n">reload_model</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">new_name_lower</span><span class="p">,</span> <span class="n">delay</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">database_forwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">):</span>
        <span class="n">new_model</span> <span class="o">=</span> <span class="n">to_state</span><span class="o">.</span><span class="n">apps</span><span class="o">.</span><span class="n">get_model</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">new_name</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">allow_migrate_model</span><span class="p">(</span><span class="n">schema_editor</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">alias</span><span class="p">,</span> <span class="n">new_model</span><span class="p">):</span>
            <span class="n">old_model</span> <span class="o">=</span> <span class="n">from_state</span><span class="o">.</span><span class="n">apps</span><span class="o">.</span><span class="n">get_model</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">old_name</span><span class="p">)</span>
            <span class="c1"># Move the main table</span>
            <span class="n">schema_editor</span><span class="o">.</span><span class="n">alter_db_table</span><span class="p">(</span>
                <span class="n">new_model</span><span class="p">,</span>
                <span class="n">old_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span>
                <span class="n">new_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span>
            <span class="p">)</span>
            <span class="c1"># Alter the fields pointing to us</span>
            <span class="k">for</span> <span class="n">related_object</span> <span class="ow">in</span> <span class="n">old_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">related_objects</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">related_object</span><span class="o">.</span><span class="n">related_model</span> <span class="o">==</span> <span class="n">old_model</span><span class="p">:</span>
                    <span class="n">model</span> <span class="o">=</span> <span class="n">new_model</span>
                    <span class="n">related_key</span> <span class="o">=</span> <span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">new_name_lower</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">model</span> <span class="o">=</span> <span class="n">related_object</span><span class="o">.</span><span class="n">related_model</span>
                    <span class="n">related_key</span> <span class="o">=</span> <span class="p">(</span>
                        <span class="n">related_object</span><span class="o">.</span><span class="n">related_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span>
                        <span class="n">related_object</span><span class="o">.</span><span class="n">related_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">model_name</span><span class="p">,</span>
                    <span class="p">)</span>
                <span class="n">to_field</span> <span class="o">=</span> <span class="n">to_state</span><span class="o">.</span><span class="n">apps</span><span class="o">.</span><span class="n">get_model</span><span class="p">(</span>
                    <span class="o">*</span><span class="n">related_key</span>
                <span class="p">)</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">related_object</span><span class="o">.</span><span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
                <span class="n">schema_editor</span><span class="o">.</span><span class="n">alter_field</span><span class="p">(</span>
                    <span class="n">model</span><span class="p">,</span>
                    <span class="n">related_object</span><span class="o">.</span><span class="n">field</span><span class="p">,</span>
                    <span class="n">to_field</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="c1"># Rename M2M fields whose name is based on this model&#39;s name.</span>
            <span class="n">fields</span> <span class="o">=</span> <span class="nb">zip</span><span class="p">(</span><span class="n">old_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_many_to_many</span><span class="p">,</span> <span class="n">new_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_many_to_many</span><span class="p">)</span>
            <span class="k">for</span> <span class="p">(</span><span class="n">old_field</span><span class="p">,</span> <span class="n">new_field</span><span class="p">)</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">:</span>
                <span class="c1"># Skip self-referential fields as these are renamed above.</span>
                <span class="k">if</span> <span class="n">new_field</span><span class="o">.</span><span class="n">model</span> <span class="o">==</span> <span class="n">new_field</span><span class="o">.</span><span class="n">related_model</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">new_field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">auto_created</span><span class="p">:</span>
                    <span class="k">continue</span>
                <span class="c1"># Rename the M2M table that&#39;s based on this model&#39;s name.</span>
                <span class="n">old_m2m_model</span> <span class="o">=</span> <span class="n">old_field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span>
                <span class="n">new_m2m_model</span> <span class="o">=</span> <span class="n">new_field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span>
                <span class="n">schema_editor</span><span class="o">.</span><span class="n">alter_db_table</span><span class="p">(</span>
                    <span class="n">new_m2m_model</span><span class="p">,</span>
                    <span class="n">old_m2m_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span>
                    <span class="n">new_m2m_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span>
                <span class="p">)</span>
                <span class="c1"># Rename the column in the M2M table that&#39;s based on this</span>
                <span class="c1"># model&#39;s name.</span>
                <span class="n">schema_editor</span><span class="o">.</span><span class="n">alter_field</span><span class="p">(</span>
                    <span class="n">new_m2m_model</span><span class="p">,</span>
                    <span class="n">old_m2m_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">old_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">model_name</span><span class="p">),</span>
                    <span class="n">new_m2m_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">new_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">model_name</span><span class="p">),</span>
                <span class="p">)</span>

    <span class="k">def</span> <span class="nf">database_backwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">new_name_lower</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">old_name_lower</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">old_name_lower</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">new_name_lower</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">new_name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">old_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">old_name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">new_name</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">database_forwards</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">new_name_lower</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">old_name_lower</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">old_name_lower</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">new_name_lower</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">new_name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">old_name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">old_name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">new_name</span>

    <span class="k">def</span> <span class="nf">references_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">app_label</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="n">name</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">old_name_lower</span> <span class="ow">or</span>
            <span class="n">name</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">new_name_lower</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">describe</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;Rename model </span><span class="si">%s</span><span class="s2"> to </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">old_name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">new_name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">reduce</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">operation</span><span class="p">,</span> <span class="n">in_between</span><span class="p">,</span> <span class="n">app_label</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">operation</span><span class="p">,</span> <span class="n">RenameModel</span><span class="p">)</span> <span class="ow">and</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">new_name_lower</span> <span class="o">==</span> <span class="n">operation</span><span class="o">.</span><span class="n">old_name_lower</span><span class="p">):</span>
            <span class="k">return</span> <span class="p">[</span>
                <span class="n">RenameModel</span><span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">old_name</span><span class="p">,</span>
                    <span class="n">operation</span><span class="o">.</span><span class="n">new_name</span><span class="p">,</span>
                <span class="p">),</span>
            <span class="p">]</span>
        <span class="c1"># Skip `ModelOperation.reduce` as we want to run `references_model`</span>
        <span class="c1"># against self.new_name.</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="nb">super</span><span class="p">(</span><span class="n">ModelOperation</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">reduce</span><span class="p">(</span><span class="n">operation</span><span class="p">,</span> <span class="n">in_between</span><span class="p">,</span> <span class="n">app_label</span><span class="o">=</span><span class="n">app_label</span><span class="p">)</span> <span class="ow">or</span>
            <span class="ow">not</span> <span class="n">operation</span><span class="o">.</span><span class="n">references_model</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">new_name</span><span class="p">,</span> <span class="n">app_label</span><span class="p">)</span>
        <span class="p">)</span></div>


<div class="viewcode-block" id="AlterModelTable"><a class="viewcode-back" href="../../../../../ref/migration-operations.html#django.db.migrations.operations.AlterModelTable">[docs]</a><span class="k">class</span> <span class="nc">AlterModelTable</span><span class="p">(</span><span class="n">ModelOperation</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Renames a model&#39;s table</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">table</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">table</span> <span class="o">=</span> <span class="n">table</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">AlterModelTable</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">deconstruct</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">kwargs</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
            <span class="s1">&#39;table&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">table</span><span class="p">,</span>
        <span class="p">}</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
            <span class="p">[],</span>
            <span class="n">kwargs</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">state_forwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">state</span><span class="p">):</span>
        <span class="n">state</span><span class="o">.</span><span class="n">models</span><span class="p">[</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name_lower</span><span class="p">]</span><span class="o">.</span><span class="n">options</span><span class="p">[</span><span class="s2">&quot;db_table&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">table</span>
        <span class="n">state</span><span class="o">.</span><span class="n">reload_model</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name_lower</span><span class="p">,</span> <span class="n">delay</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">database_forwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">):</span>
        <span class="n">new_model</span> <span class="o">=</span> <span class="n">to_state</span><span class="o">.</span><span class="n">apps</span><span class="o">.</span><span class="n">get_model</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">allow_migrate_model</span><span class="p">(</span><span class="n">schema_editor</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">alias</span><span class="p">,</span> <span class="n">new_model</span><span class="p">):</span>
            <span class="n">old_model</span> <span class="o">=</span> <span class="n">from_state</span><span class="o">.</span><span class="n">apps</span><span class="o">.</span><span class="n">get_model</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
            <span class="n">schema_editor</span><span class="o">.</span><span class="n">alter_db_table</span><span class="p">(</span>
                <span class="n">new_model</span><span class="p">,</span>
                <span class="n">old_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span>
                <span class="n">new_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span>
            <span class="p">)</span>
            <span class="c1"># Rename M2M fields whose name is based on this model&#39;s db_table</span>
            <span class="k">for</span> <span class="p">(</span><span class="n">old_field</span><span class="p">,</span> <span class="n">new_field</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">old_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_many_to_many</span><span class="p">,</span> <span class="n">new_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">local_many_to_many</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">new_field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">auto_created</span><span class="p">:</span>
                    <span class="n">schema_editor</span><span class="o">.</span><span class="n">alter_db_table</span><span class="p">(</span>
                        <span class="n">new_field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="p">,</span>
                        <span class="n">old_field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span>
                        <span class="n">new_field</span><span class="o">.</span><span class="n">remote_field</span><span class="o">.</span><span class="n">through</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">,</span>
                    <span class="p">)</span>

    <span class="k">def</span> <span class="nf">database_backwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">database_forwards</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">describe</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;Rename table for </span><span class="si">%s</span><span class="s2"> to </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">table</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">table</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="s2">&quot;(default)&quot;</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">reduce</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">operation</span><span class="p">,</span> <span class="n">in_between</span><span class="p">,</span> <span class="n">app_label</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">operation</span><span class="p">,</span> <span class="p">(</span><span class="n">AlterModelTable</span><span class="p">,</span> <span class="n">DeleteModel</span><span class="p">))</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">name_lower</span> <span class="o">==</span> <span class="n">operation</span><span class="o">.</span><span class="n">name_lower</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[</span><span class="n">operation</span><span class="p">]</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">AlterModelTable</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">reduce</span><span class="p">(</span><span class="n">operation</span><span class="p">,</span> <span class="n">in_between</span><span class="p">,</span> <span class="n">app_label</span><span class="o">=</span><span class="n">app_label</span><span class="p">)</span></div>


<span class="k">class</span> <span class="nc">ModelOptionOperation</span><span class="p">(</span><span class="n">ModelOperation</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">reduce</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">operation</span><span class="p">,</span> <span class="n">in_between</span><span class="p">,</span> <span class="n">app_label</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">operation</span><span class="p">,</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,</span> <span class="n">DeleteModel</span><span class="p">))</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">name_lower</span> <span class="o">==</span> <span class="n">operation</span><span class="o">.</span><span class="n">name_lower</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[</span><span class="n">operation</span><span class="p">]</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">ModelOptionOperation</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">reduce</span><span class="p">(</span><span class="n">operation</span><span class="p">,</span> <span class="n">in_between</span><span class="p">,</span> <span class="n">app_label</span><span class="o">=</span><span class="n">app_label</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">FieldRelatedOptionOperation</span><span class="p">(</span><span class="n">ModelOptionOperation</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">reduce</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">operation</span><span class="p">,</span> <span class="n">in_between</span><span class="p">,</span> <span class="n">app_label</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">operation</span><span class="p">,</span> <span class="n">FieldOperation</span><span class="p">)</span> <span class="ow">and</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">name_lower</span> <span class="o">==</span> <span class="n">operation</span><span class="o">.</span><span class="n">model_name_lower</span> <span class="ow">and</span>
                <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">references_field</span><span class="p">(</span><span class="n">operation</span><span class="o">.</span><span class="n">model_name</span><span class="p">,</span> <span class="n">operation</span><span class="o">.</span><span class="n">name</span><span class="p">)):</span>
            <span class="k">return</span> <span class="p">[</span><span class="n">operation</span><span class="p">,</span> <span class="bp">self</span><span class="p">]</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">FieldRelatedOptionOperation</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">reduce</span><span class="p">(</span><span class="n">operation</span><span class="p">,</span> <span class="n">in_between</span><span class="p">,</span> <span class="n">app_label</span><span class="o">=</span><span class="n">app_label</span><span class="p">)</span>


<div class="viewcode-block" id="AlterUniqueTogether"><a class="viewcode-back" href="../../../../../ref/migration-operations.html#django.db.migrations.operations.AlterUniqueTogether">[docs]</a><span class="k">class</span> <span class="nc">AlterUniqueTogether</span><span class="p">(</span><span class="n">FieldRelatedOptionOperation</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Changes the value of unique_together to the target one.</span>
<span class="sd">    Input value of unique_together must be a set of tuples.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">option_name</span> <span class="o">=</span> <span class="s2">&quot;unique_together&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">unique_together</span><span class="p">):</span>
        <span class="n">unique_together</span> <span class="o">=</span> <span class="n">normalize_together</span><span class="p">(</span><span class="n">unique_together</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">unique_together</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="nb">tuple</span><span class="p">(</span><span class="n">cons</span><span class="p">)</span> <span class="k">for</span> <span class="n">cons</span> <span class="ow">in</span> <span class="n">unique_together</span><span class="p">)</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">AlterUniqueTogether</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">deconstruct</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">kwargs</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
            <span class="s1">&#39;unique_together&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">unique_together</span><span class="p">,</span>
        <span class="p">}</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
            <span class="p">[],</span>
            <span class="n">kwargs</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">state_forwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">state</span><span class="p">):</span>
        <span class="n">model_state</span> <span class="o">=</span> <span class="n">state</span><span class="o">.</span><span class="n">models</span><span class="p">[</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name_lower</span><span class="p">]</span>
        <span class="n">model_state</span><span class="o">.</span><span class="n">options</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">option_name</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">unique_together</span>
        <span class="n">state</span><span class="o">.</span><span class="n">reload_model</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name_lower</span><span class="p">,</span> <span class="n">delay</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">database_forwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">):</span>
        <span class="n">new_model</span> <span class="o">=</span> <span class="n">to_state</span><span class="o">.</span><span class="n">apps</span><span class="o">.</span><span class="n">get_model</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">allow_migrate_model</span><span class="p">(</span><span class="n">schema_editor</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">alias</span><span class="p">,</span> <span class="n">new_model</span><span class="p">):</span>
            <span class="n">old_model</span> <span class="o">=</span> <span class="n">from_state</span><span class="o">.</span><span class="n">apps</span><span class="o">.</span><span class="n">get_model</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
            <span class="n">schema_editor</span><span class="o">.</span><span class="n">alter_unique_together</span><span class="p">(</span>
                <span class="n">new_model</span><span class="p">,</span>
                <span class="nb">getattr</span><span class="p">(</span><span class="n">old_model</span><span class="o">.</span><span class="n">_meta</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">option_name</span><span class="p">,</span> <span class="nb">set</span><span class="p">()),</span>
                <span class="nb">getattr</span><span class="p">(</span><span class="n">new_model</span><span class="o">.</span><span class="n">_meta</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">option_name</span><span class="p">,</span> <span class="nb">set</span><span class="p">()),</span>
            <span class="p">)</span>

    <span class="k">def</span> <span class="nf">database_backwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">database_forwards</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">references_field</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model_name</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">app_label</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">references_model</span><span class="p">(</span><span class="n">model_name</span><span class="p">,</span> <span class="n">app_label</span><span class="p">)</span> <span class="ow">and</span>
            <span class="p">(</span>
                <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">unique_together</span> <span class="ow">or</span>
                <span class="nb">any</span><span class="p">((</span><span class="n">name</span> <span class="ow">in</span> <span class="n">together</span><span class="p">)</span> <span class="k">for</span> <span class="n">together</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">unique_together</span><span class="p">)</span>
            <span class="p">)</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">describe</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;Alter </span><span class="si">%s</span><span class="s2"> for </span><span class="si">%s</span><span class="s2"> (</span><span class="si">%s</span><span class="s2"> constraint(s))&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">option_name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">unique_together</span> <span class="ow">or</span> <span class="s1">&#39;&#39;</span><span class="p">))</span></div>


<div class="viewcode-block" id="AlterIndexTogether"><a class="viewcode-back" href="../../../../../ref/migration-operations.html#django.db.migrations.operations.AlterIndexTogether">[docs]</a><span class="k">class</span> <span class="nc">AlterIndexTogether</span><span class="p">(</span><span class="n">FieldRelatedOptionOperation</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Changes the value of index_together to the target one.</span>
<span class="sd">    Input value of index_together must be a set of tuples.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">option_name</span> <span class="o">=</span> <span class="s2">&quot;index_together&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">index_together</span><span class="p">):</span>
        <span class="n">index_together</span> <span class="o">=</span> <span class="n">normalize_together</span><span class="p">(</span><span class="n">index_together</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">index_together</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="nb">tuple</span><span class="p">(</span><span class="n">cons</span><span class="p">)</span> <span class="k">for</span> <span class="n">cons</span> <span class="ow">in</span> <span class="n">index_together</span><span class="p">)</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">AlterIndexTogether</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">deconstruct</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">kwargs</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
            <span class="s1">&#39;index_together&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">index_together</span><span class="p">,</span>
        <span class="p">}</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
            <span class="p">[],</span>
            <span class="n">kwargs</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">state_forwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">state</span><span class="p">):</span>
        <span class="n">model_state</span> <span class="o">=</span> <span class="n">state</span><span class="o">.</span><span class="n">models</span><span class="p">[</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name_lower</span><span class="p">]</span>
        <span class="n">model_state</span><span class="o">.</span><span class="n">options</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">option_name</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">index_together</span>
        <span class="n">state</span><span class="o">.</span><span class="n">reload_model</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name_lower</span><span class="p">,</span> <span class="n">delay</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">database_forwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">):</span>
        <span class="n">new_model</span> <span class="o">=</span> <span class="n">to_state</span><span class="o">.</span><span class="n">apps</span><span class="o">.</span><span class="n">get_model</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">allow_migrate_model</span><span class="p">(</span><span class="n">schema_editor</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">alias</span><span class="p">,</span> <span class="n">new_model</span><span class="p">):</span>
            <span class="n">old_model</span> <span class="o">=</span> <span class="n">from_state</span><span class="o">.</span><span class="n">apps</span><span class="o">.</span><span class="n">get_model</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
            <span class="n">schema_editor</span><span class="o">.</span><span class="n">alter_index_together</span><span class="p">(</span>
                <span class="n">new_model</span><span class="p">,</span>
                <span class="nb">getattr</span><span class="p">(</span><span class="n">old_model</span><span class="o">.</span><span class="n">_meta</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">option_name</span><span class="p">,</span> <span class="nb">set</span><span class="p">()),</span>
                <span class="nb">getattr</span><span class="p">(</span><span class="n">new_model</span><span class="o">.</span><span class="n">_meta</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">option_name</span><span class="p">,</span> <span class="nb">set</span><span class="p">()),</span>
            <span class="p">)</span>

    <span class="k">def</span> <span class="nf">database_backwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">database_forwards</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">references_field</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model_name</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">app_label</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">references_model</span><span class="p">(</span><span class="n">model_name</span><span class="p">,</span> <span class="n">app_label</span><span class="p">)</span> <span class="ow">and</span>
            <span class="p">(</span>
                <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">index_together</span> <span class="ow">or</span>
                <span class="nb">any</span><span class="p">((</span><span class="n">name</span> <span class="ow">in</span> <span class="n">together</span><span class="p">)</span> <span class="k">for</span> <span class="n">together</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">index_together</span><span class="p">)</span>
            <span class="p">)</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">describe</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;Alter </span><span class="si">%s</span><span class="s2"> for </span><span class="si">%s</span><span class="s2"> (</span><span class="si">%s</span><span class="s2"> constraint(s))&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">option_name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">index_together</span> <span class="ow">or</span> <span class="s1">&#39;&#39;</span><span class="p">))</span></div>


<div class="viewcode-block" id="AlterOrderWithRespectTo"><a class="viewcode-back" href="../../../../../ref/migration-operations.html#django.db.migrations.operations.AlterOrderWithRespectTo">[docs]</a><span class="k">class</span> <span class="nc">AlterOrderWithRespectTo</span><span class="p">(</span><span class="n">FieldRelatedOptionOperation</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Represents a change with the order_with_respect_to option.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">order_with_respect_to</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">order_with_respect_to</span> <span class="o">=</span> <span class="n">order_with_respect_to</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">AlterOrderWithRespectTo</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">deconstruct</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">kwargs</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
            <span class="s1">&#39;order_with_respect_to&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">order_with_respect_to</span><span class="p">,</span>
        <span class="p">}</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
            <span class="p">[],</span>
            <span class="n">kwargs</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">state_forwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">state</span><span class="p">):</span>
        <span class="n">model_state</span> <span class="o">=</span> <span class="n">state</span><span class="o">.</span><span class="n">models</span><span class="p">[</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name_lower</span><span class="p">]</span>
        <span class="n">model_state</span><span class="o">.</span><span class="n">options</span><span class="p">[</span><span class="s1">&#39;order_with_respect_to&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">order_with_respect_to</span>
        <span class="n">state</span><span class="o">.</span><span class="n">reload_model</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name_lower</span><span class="p">,</span> <span class="n">delay</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">database_forwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">):</span>
        <span class="n">to_model</span> <span class="o">=</span> <span class="n">to_state</span><span class="o">.</span><span class="n">apps</span><span class="o">.</span><span class="n">get_model</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">allow_migrate_model</span><span class="p">(</span><span class="n">schema_editor</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">alias</span><span class="p">,</span> <span class="n">to_model</span><span class="p">):</span>
            <span class="n">from_model</span> <span class="o">=</span> <span class="n">from_state</span><span class="o">.</span><span class="n">apps</span><span class="o">.</span><span class="n">get_model</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
            <span class="c1"># Remove a field if we need to</span>
            <span class="k">if</span> <span class="n">from_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">order_with_respect_to</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">to_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">order_with_respect_to</span><span class="p">:</span>
                <span class="n">schema_editor</span><span class="o">.</span><span class="n">remove_field</span><span class="p">(</span><span class="n">from_model</span><span class="p">,</span> <span class="n">from_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="s2">&quot;_order&quot;</span><span class="p">))</span>
            <span class="c1"># Add a field if we need to (altering the column is untouched as</span>
            <span class="c1"># it&#39;s likely a rename)</span>
            <span class="k">elif</span> <span class="n">to_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">order_with_respect_to</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">from_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">order_with_respect_to</span><span class="p">:</span>
                <span class="n">field</span> <span class="o">=</span> <span class="n">to_model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="s2">&quot;_order&quot;</span><span class="p">)</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">field</span><span class="o">.</span><span class="n">has_default</span><span class="p">():</span>
                    <span class="n">field</span><span class="o">.</span><span class="n">default</span> <span class="o">=</span> <span class="mi">0</span>
                <span class="n">schema_editor</span><span class="o">.</span><span class="n">add_field</span><span class="p">(</span>
                    <span class="n">from_model</span><span class="p">,</span>
                    <span class="n">field</span><span class="p">,</span>
                <span class="p">)</span>

    <span class="k">def</span> <span class="nf">database_backwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">database_forwards</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">references_field</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model_name</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">app_label</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">references_model</span><span class="p">(</span><span class="n">model_name</span><span class="p">,</span> <span class="n">app_label</span><span class="p">)</span> <span class="ow">and</span>
            <span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">order_with_respect_to</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span>
                <span class="n">name</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">order_with_respect_to</span>
            <span class="p">)</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">describe</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;Set order_with_respect_to on </span><span class="si">%s</span><span class="s2"> to </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">order_with_respect_to</span><span class="p">)</span></div>


<div class="viewcode-block" id="AlterModelOptions"><a class="viewcode-back" href="../../../../../ref/migration-operations.html#django.db.migrations.operations.AlterModelOptions">[docs]</a><span class="k">class</span> <span class="nc">AlterModelOptions</span><span class="p">(</span><span class="n">ModelOptionOperation</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Sets new model options that don&#39;t directly affect the database schema</span>
<span class="sd">    (like verbose_name, permissions, ordering). Python code in migrations</span>
<span class="sd">    may still need them.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># Model options we want to compare and preserve in an AlterModelOptions op</span>
    <span class="n">ALTER_OPTION_KEYS</span> <span class="o">=</span> <span class="p">[</span>
        <span class="s2">&quot;base_manager_name&quot;</span><span class="p">,</span>
        <span class="s2">&quot;default_manager_name&quot;</span><span class="p">,</span>
        <span class="s2">&quot;get_latest_by&quot;</span><span class="p">,</span>
        <span class="s2">&quot;managed&quot;</span><span class="p">,</span>
        <span class="s2">&quot;ordering&quot;</span><span class="p">,</span>
        <span class="s2">&quot;permissions&quot;</span><span class="p">,</span>
        <span class="s2">&quot;default_permissions&quot;</span><span class="p">,</span>
        <span class="s2">&quot;select_on_save&quot;</span><span class="p">,</span>
        <span class="s2">&quot;verbose_name&quot;</span><span class="p">,</span>
        <span class="s2">&quot;verbose_name_plural&quot;</span><span class="p">,</span>
    <span class="p">]</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">options</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">options</span> <span class="o">=</span> <span class="n">options</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">AlterModelOptions</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">deconstruct</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">kwargs</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
            <span class="s1">&#39;options&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="p">,</span>
        <span class="p">}</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
            <span class="p">[],</span>
            <span class="n">kwargs</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">state_forwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">state</span><span class="p">):</span>
        <span class="n">model_state</span> <span class="o">=</span> <span class="n">state</span><span class="o">.</span><span class="n">models</span><span class="p">[</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name_lower</span><span class="p">]</span>
        <span class="n">model_state</span><span class="o">.</span><span class="n">options</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">model_state</span><span class="o">.</span><span class="n">options</span><span class="p">)</span>
        <span class="n">model_state</span><span class="o">.</span><span class="n">options</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">options</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">ALTER_OPTION_KEYS</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">key</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">options</span> <span class="ow">and</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">model_state</span><span class="o">.</span><span class="n">options</span><span class="p">:</span>
                <span class="k">del</span> <span class="n">model_state</span><span class="o">.</span><span class="n">options</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
        <span class="n">state</span><span class="o">.</span><span class="n">reload_model</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name_lower</span><span class="p">,</span> <span class="n">delay</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">database_forwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">database_backwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">describe</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;Change Meta options on </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="p">)</span></div>


<div class="viewcode-block" id="AlterModelManagers"><a class="viewcode-back" href="../../../../../ref/migration-operations.html#django.db.migrations.operations.AlterModelManagers">[docs]</a><span class="k">class</span> <span class="nc">AlterModelManagers</span><span class="p">(</span><span class="n">ModelOptionOperation</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Alters the model&#39;s managers</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">serialization_expand_args</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;managers&#39;</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">managers</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">managers</span> <span class="o">=</span> <span class="n">managers</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">AlterModelManagers</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">deconstruct</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
            <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">managers</span><span class="p">],</span>
            <span class="p">{}</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">state_forwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">state</span><span class="p">):</span>
        <span class="n">model_state</span> <span class="o">=</span> <span class="n">state</span><span class="o">.</span><span class="n">models</span><span class="p">[</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name_lower</span><span class="p">]</span>
        <span class="n">model_state</span><span class="o">.</span><span class="n">managers</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">managers</span><span class="p">)</span>
        <span class="n">state</span><span class="o">.</span><span class="n">reload_model</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name_lower</span><span class="p">,</span> <span class="n">delay</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">database_forwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">database_backwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">describe</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;Change managers on </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="p">)</span></div>


<span class="k">class</span> <span class="nc">IndexOperation</span><span class="p">(</span><span class="n">Operation</span><span class="p">):</span>
    <span class="n">option_name</span> <span class="o">=</span> <span class="s1">&#39;indexes&#39;</span>

    <span class="nd">@cached_property</span>
    <span class="k">def</span> <span class="nf">model_name_lower</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_name</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>


<div class="viewcode-block" id="AddIndex"><a class="viewcode-back" href="../../../../../ref/migration-operations.html#django.db.migrations.operations.AddIndex">[docs]</a><span class="k">class</span> <span class="nc">AddIndex</span><span class="p">(</span><span class="n">IndexOperation</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Add an index on a model.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model_name</span><span class="p">,</span> <span class="n">index</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model_name</span> <span class="o">=</span> <span class="n">model_name</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">index</span><span class="o">.</span><span class="n">name</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;Indexes passed to AddIndex operations require a name &quot;</span>
                <span class="s2">&quot;argument. </span><span class="si">%r</span><span class="s2"> doesn&#39;t have one.&quot;</span> <span class="o">%</span> <span class="n">index</span>
            <span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">index</span> <span class="o">=</span> <span class="n">index</span>

    <span class="k">def</span> <span class="nf">state_forwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">state</span><span class="p">):</span>
        <span class="n">model_state</span> <span class="o">=</span> <span class="n">state</span><span class="o">.</span><span class="n">models</span><span class="p">[</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_name_lower</span><span class="p">]</span>
        <span class="n">indexes</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">model_state</span><span class="o">.</span><span class="n">options</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">option_name</span><span class="p">])</span>
        <span class="n">indexes</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">clone</span><span class="p">())</span>
        <span class="n">model_state</span><span class="o">.</span><span class="n">options</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">option_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">indexes</span>
        <span class="n">state</span><span class="o">.</span><span class="n">reload_model</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_name_lower</span><span class="p">,</span> <span class="n">delay</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">database_forwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">):</span>
        <span class="n">model</span> <span class="o">=</span> <span class="n">to_state</span><span class="o">.</span><span class="n">apps</span><span class="o">.</span><span class="n">get_model</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_name</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">allow_migrate_model</span><span class="p">(</span><span class="n">schema_editor</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">alias</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
            <span class="n">schema_editor</span><span class="o">.</span><span class="n">add_index</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">index</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">database_backwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">):</span>
        <span class="n">model</span> <span class="o">=</span> <span class="n">from_state</span><span class="o">.</span><span class="n">apps</span><span class="o">.</span><span class="n">get_model</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_name</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">allow_migrate_model</span><span class="p">(</span><span class="n">schema_editor</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">alias</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
            <span class="n">schema_editor</span><span class="o">.</span><span class="n">remove_index</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">index</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">deconstruct</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">kwargs</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;model_name&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_name</span><span class="p">,</span>
            <span class="s1">&#39;index&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">index</span><span class="p">,</span>
        <span class="p">}</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
            <span class="p">[],</span>
            <span class="n">kwargs</span><span class="p">,</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">describe</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39;Create index </span><span class="si">%s</span><span class="s1"> on field(s) </span><span class="si">%s</span><span class="s1"> of model </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
            <span class="s1">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">fields</span><span class="p">),</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">model_name</span><span class="p">,</span>
        <span class="p">)</span></div>


<div class="viewcode-block" id="RemoveIndex"><a class="viewcode-back" href="../../../../../ref/migration-operations.html#django.db.migrations.operations.RemoveIndex">[docs]</a><span class="k">class</span> <span class="nc">RemoveIndex</span><span class="p">(</span><span class="n">IndexOperation</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Remove an index from a model.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model_name</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model_name</span> <span class="o">=</span> <span class="n">model_name</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>

    <span class="k">def</span> <span class="nf">state_forwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">state</span><span class="p">):</span>
        <span class="n">model_state</span> <span class="o">=</span> <span class="n">state</span><span class="o">.</span><span class="n">models</span><span class="p">[</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_name_lower</span><span class="p">]</span>
        <span class="n">indexes</span> <span class="o">=</span> <span class="n">model_state</span><span class="o">.</span><span class="n">options</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">option_name</span><span class="p">]</span>
        <span class="n">model_state</span><span class="o">.</span><span class="n">options</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">option_name</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">idx</span> <span class="k">for</span> <span class="n">idx</span> <span class="ow">in</span> <span class="n">indexes</span> <span class="k">if</span> <span class="n">idx</span><span class="o">.</span><span class="n">name</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">]</span>
        <span class="n">state</span><span class="o">.</span><span class="n">reload_model</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_name_lower</span><span class="p">,</span> <span class="n">delay</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">database_forwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">):</span>
        <span class="n">model</span> <span class="o">=</span> <span class="n">from_state</span><span class="o">.</span><span class="n">apps</span><span class="o">.</span><span class="n">get_model</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_name</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">allow_migrate_model</span><span class="p">(</span><span class="n">schema_editor</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">alias</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
            <span class="n">from_model_state</span> <span class="o">=</span> <span class="n">from_state</span><span class="o">.</span><span class="n">models</span><span class="p">[</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_name_lower</span><span class="p">]</span>
            <span class="n">index</span> <span class="o">=</span> <span class="n">from_model_state</span><span class="o">.</span><span class="n">get_index_by_name</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
            <span class="n">schema_editor</span><span class="o">.</span><span class="n">remove_index</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">index</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">database_backwards</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">,</span> <span class="n">from_state</span><span class="p">,</span> <span class="n">to_state</span><span class="p">):</span>
        <span class="n">model</span> <span class="o">=</span> <span class="n">to_state</span><span class="o">.</span><span class="n">apps</span><span class="o">.</span><span class="n">get_model</span><span class="p">(</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_name</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">allow_migrate_model</span><span class="p">(</span><span class="n">schema_editor</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">alias</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
            <span class="n">to_model_state</span> <span class="o">=</span> <span class="n">to_state</span><span class="o">.</span><span class="n">models</span><span class="p">[</span><span class="n">app_label</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_name_lower</span><span class="p">]</span>
            <span class="n">index</span> <span class="o">=</span> <span class="n">to_model_state</span><span class="o">.</span><span class="n">get_index_by_name</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
            <span class="n">schema_editor</span><span class="o">.</span><span class="n">add_index</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">index</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">deconstruct</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">kwargs</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s1">&#39;model_name&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_name</span><span class="p">,</span>
            <span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
        <span class="p">}</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
            <span class="p">[],</span>
            <span class="n">kwargs</span><span class="p">,</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">describe</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39;Remove index </span><span class="si">%s</span><span class="s1"> from </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_name</span><span class="p">)</span></div>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Jun 03, 2019</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>